package com.xique.park.controller.web;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.domain.ParkGarage;
import com.xique.park.bean.domain.ParkLane;
import com.xique.park.bean.request.web.garageLaneDev.*;
import com.xique.park.bean.request.web.garageLaneDev.board.BoardInfoRequest;
import com.xique.park.bean.request.web.garageLaneDev.board.LedSetRequest;
import com.xique.park.bean.request.web.garageLaneDev.board.VoiceSetRequest;
import com.xique.park.bean.request.web.garageLaneDev.board.VoiceTestRequest;
import com.xique.park.bean.request.web.garageLaneDev.garage.AddGarageRequest;
import com.xique.park.bean.request.web.garageLaneDev.garage.DeleteGarageRequest;
import com.xique.park.bean.request.web.garageLaneDev.garage.EditGarageRequest;
import com.xique.park.bean.request.web.garageLaneDev.ipc.AddIpcRequest;
import com.xique.park.bean.request.web.garageLaneDev.ipc.DeleteIpcRequest;
import com.xique.park.bean.request.web.garageLaneDev.ipc.EditIpcRequest;
import com.xique.park.bean.request.web.garageLaneDev.lane.AddLaneRequest;
import com.xique.park.bean.request.web.garageLaneDev.lane.DeleteLaneRequest;
import com.xique.park.bean.request.web.garageLaneDev.lane.EditLaneRequest;
import com.xique.park.bean.request.web.garageLaneDev.subdev.AddSubDevRequest;
import com.xique.park.bean.request.web.garageLaneDev.subdev.DeleteSubDevRequest;
import com.xique.park.bean.request.web.garageLaneDev.subdev.EditSubDevRequest;
import com.xique.park.service.biz.service.IParkGarageLaneDevBizService;
import com.xique.park.service.service.IParkLaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 车库通道设备controller
 *
 * @author liuxueli
 * @Date 2021/12/24
 */
@RestController
@RequestMapping("/web/garageLaneDev")
public class WebParkGarageLaneDeviceController extends BaseController {

    @Autowired
    private IParkGarageLaneDevBizService parkGarageLaneDevBizService;

    @Autowired
    private IParkLaneService parkLaneService;

    /**
     * 查询车库通道设备树形列表
     */
    @PostMapping("/treeList")
    @PreAuthorize(hasPermi = "park:garageLaneDev:list")
    public AjaxResult treeList(@Validated @RequestBody GarageLaneDevListRequest request) {
        return parkGarageLaneDevBizService.getTreeList(request);
    }

    /**
     * 获取树节点信息
     */
    @PreAuthorize(hasPermi = "park:garageLaneDev:query")
    @PostMapping("/getNodeInfo")
    public AjaxResult getNodeInfo(@Validated @RequestBody GarageLaneDevNodeInfoRequest request) {
        return parkGarageLaneDevBizService.getNodeInfo(request);
    }

    /* ------------------------------------- 车库 -------------------------------------*/

    /**
     * 新增车库
     */
    @PreAuthorize(hasPermi = "park:garage:add")
    @Log(title = "车库", businessType = BusinessType.INSERT)
    @PostMapping("/addGarage")
    public AjaxResult addGarage(@Validated @RequestBody AddGarageRequest request) {
        return parkGarageLaneDevBizService.addGarage(request);
    }

    /**
     * 修改车库
     */
    @PreAuthorize(hasPermi = "park:garage:edit")
    @Log(title = "车库", businessType = BusinessType.UPDATE)
    @PostMapping("/editGarage")
    public AjaxResult edit(@Validated @RequestBody EditGarageRequest request) {
        return parkGarageLaneDevBizService.editGarage(request);
    }

    /**
     * 删除车库
     */
    @PreAuthorize(hasPermi = "park:garage:remove")
    @Log(title = "车库", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteGarage")
    public AjaxResult deleteGarage(@Validated @RequestBody DeleteGarageRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return parkGarageLaneDevBizService.deleteGarage(request);
    }

    /**
     * 获取车辆管理类型列表
     */
//    @PreAuthorize(hasPermi = "park:garageLaneDev:query")
    @GetMapping("/getMngTypeList")
    public AjaxResult getMngTypeList() {
        return parkGarageLaneDevBizService.getMngTypeList();
    }


    /* ------------------------------------- 通道 ------------------------------------- */

    /**
     * 新增通道
     */
    @PreAuthorize(hasPermi = "park:lane:add")
    @Log(title = "通道", businessType = BusinessType.INSERT)
    @PostMapping("/addLane")
    public AjaxResult addLane(@Validated @RequestBody AddLaneRequest request) {
        return parkGarageLaneDevBizService.addLane(request);
    }

    /**
     * 编辑通道
     */
    @PreAuthorize(hasPermi = "park:lane:edit")
    @Log(title = "通道", businessType = BusinessType.UPDATE)
    @PostMapping("/editLane")
    public AjaxResult editLane(@Validated @RequestBody EditLaneRequest request) {
        return parkGarageLaneDevBizService.editLane(request);
    }

    /**
     * 删除通道
     */
    @PreAuthorize(hasPermi = "park:lane:remove")
    @Log(title = "通道", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteLane")
    public AjaxResult deleteLane(@Validated @RequestBody DeleteLaneRequest request) {
        return parkGarageLaneDevBizService.deleteLane(request);
    }

    /**
     * 获取车库列表
     */
    @GetMapping("/getGarageList")
    public AjaxResult getGarageList(ParkGarage parkGarage) {
        return parkGarageLaneDevBizService.getGarageList(parkGarage);
    }

    /**
     * 获取通道二维码url
     */
    @PreAuthorize(hasPermi = "park:lane:query")
    @PostMapping(value = "/generateQrcode")
    public AjaxResult getPrePayUrl(@Validated @RequestBody DeleteLaneRequest request) {
        Long parkId = SecurityUtils.getParkId();
        if (request.getLaneType() == null) {
            return AjaxResult.error("通道类型不可为空");
        }
        if (null == parkId) {
            return AjaxResult.error("车场ID不能为空");
        }
        request.setParkId(parkId);
        return parkGarageLaneDevBizService.generateQrcode(request);
    }


    /* ------------------------------------- 主相机 ------------------------------------- */

    /**
     * 新增主相机
     */
    @PreAuthorize(hasPermi = "park:ipc:add")
    @Log(title = "主相机", businessType = BusinessType.INSERT)
    @PostMapping("/addIpc")
    public AjaxResult addIpc(@Validated @RequestBody AddIpcRequest request) {
        // 对序列号做校验，符合臻视的正则才能保存主相机
        String pattern = "^[\\da-fA-F]{8}-[\\da-fA-F]{8}$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(request.getDevCode());
        if (!m.matches()) {
            return AjaxResult.error("设备序列号不合规！");
        }
        return parkGarageLaneDevBizService.addIpc(request);
    }

    /**
     * 编辑主相机
     */
    @PreAuthorize(hasPermi = "park:ipc:edit")
    @Log(title = "主相机", businessType = BusinessType.UPDATE)
    @PostMapping("/editIpc")
    public AjaxResult editIpc(@Validated @RequestBody EditIpcRequest request) {
        String pattern = "^[\\da-fA-F]{8}-[\\da-fA-F]{8}$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(request.getDevCode());
        if (!m.matches()) {
            return AjaxResult.error("设备序列号不合规！");
        }
        return parkGarageLaneDevBizService.editIpc(request);
    }

    /**
     * 删除主相机
     */
    @PreAuthorize(hasPermi = "park:ipc:remove")
    @Log(title = "主相机", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteIpc")
    public AjaxResult deleteIpc(@Validated @RequestBody DeleteIpcRequest request) {
        return parkGarageLaneDevBizService.deleteIpc(request);
    }

    /* ------------------------------------- 附加设备 ------------------------------------- */

    /**
     * 新增附加设备
     */
    @PreAuthorize(hasPermi = "park:subDev:add")
    @Log(title = "附加设备", businessType = BusinessType.INSERT)
    @PostMapping("/addSubDev")
    public AjaxResult addSubIpc(@Validated @RequestBody AddSubDevRequest request) {
        return parkGarageLaneDevBizService.addSubDev(request);
    }

    /**
     * 编辑附加设备
     */
    @PreAuthorize(hasPermi = "park:subDev:edit")
    @Log(title = "附加设备", businessType = BusinessType.UPDATE)
    @PostMapping("/editSubDev")
    public AjaxResult editSubIpc(@Validated @RequestBody EditSubDevRequest request) {
        return parkGarageLaneDevBizService.editSubDev(request);
    }

    /**
     * 删除附加设备
     */
    @PreAuthorize(hasPermi = "park:subDev:remove")
    @Log(title = "附加设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteSubDev")
    public AjaxResult deleteSubIpc(@Validated @RequestBody DeleteSubDevRequest request) {
        return parkGarageLaneDevBizService.deleteSubDev(request);
    }

    /* ------------------------------------- 语音显示屏配置 ------------------------------------- */

    /**
     * 显示屏设置信息回显
     */
    @PreAuthorize(hasPermi = "park:led:query")
    @PostMapping("/getLedConfig")
    public AjaxResult getLedConfig(@Validated @RequestBody BoardInfoRequest request) {
        return parkGarageLaneDevBizService.getLedConfig(request);
    }

    /**
     * 音量设置信息回显
     */
    @PreAuthorize(hasPermi = "park:voice:query")
    @PostMapping("/getVoiceConfig")
    public AjaxResult getVoiceConfig(@Validated @RequestBody BoardInfoRequest request) {
        return parkGarageLaneDevBizService.getVoiceConfig(request);
    }

    /**
     * 显示屏设置
     */
    @PreAuthorize(hasPermi = "park:led:edit")
    @Log(title = "显示屏设置", businessType = BusinessType.UPDATE)
    @PostMapping("/setLed")
    public AjaxResult setLed(@Validated @RequestBody LedSetRequest request) {
        return parkGarageLaneDevBizService.setLed(request);
    }

    /**
     * 音量设置
     */
    @PreAuthorize(hasPermi = "park:voice:edit")
    @Log(title = "音量设置", businessType = BusinessType.UPDATE)
    @PostMapping("/setVoice")
    public AjaxResult setLed(@Validated @RequestBody VoiceSetRequest request) {
        return parkGarageLaneDevBizService.setVoice(request);
    }

    /**
     * 音量测试
     */
    @PreAuthorize(hasPermi = "park:voice:test")
    @Log(title = "音量测试", businessType = BusinessType.COMMAND)
    @PostMapping("/voiceTest")
    public AjaxResult voiceTest(@Validated @RequestBody VoiceTestRequest request) {
        return parkGarageLaneDevBizService.voiceTest(request);
    }

    /**
     * 删除项目下的所有设备
     */
    @PreAuthorize(hasPermi = "park:subDev:remove")
    @Log(title = "附加设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteDevices")
    public AjaxResult deleteDevices(@RequestParam("parkId") Long parkId) {
        if (parkId == null){
            return  AjaxResult.error("项目ID为空");
        }
        return parkGarageLaneDevBizService.deleteDevices(parkId);
    }

    /**
     * 查询通道列表
     */
    @GetMapping("/lane/list")
    public AjaxResult list()
    {
        ParkLane parkLane = new ParkLane();
        parkLane.setParkId(SecurityUtils.getParkId());
        List<ParkLane> list = parkLaneService.selectParkLaneList(parkLane);
        return AjaxResult.success(list);
    }
}
